package Final;

import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.StringTokenizer;

import javax.imageio.ImageIO;


public class Matriz {
	private int fil, col;
	public Elemento matriz[][];
	private FileWriter Fout;
	private FileReader Fin;
	private BufferedReader BufferIn;
	private String Line;
	private PrintWriter BufferOut;
	static final int MAXFIL = 50;
	static final int MAXCOL = 50;
	static final int MINFIL = 11;
	static final int MINCOL = 11;

    public Matriz() {
            setFil(MAXFIL);
            setCol(MAXCOL);
            matriz = new Elemento [fil][col];
            for (int i = 0; i < fil; i++)
         for (int j = 0; j < col; j++) 
             matriz[i][j] = new Elemento();
    }

    //SETERS
    public void setFil(int _fil){ fil = _fil; }
    public void setCol(int _col){ col = _col; }
    public void setFilCol(int _fil, int _col){ setFil(_fil); setCol(_col); }
    
    //GETERS
    public int getFil(){ return fil; }
    public int getCol(){ return col; }
    
    //CARGAR MATRIZ DESDE FICHERO
    public boolean cargarMatriz(String namefile){
    	boolean result = true;
        int i = 0;
        int j = 0;
        try {
            Fin = new FileReader(new File(namefile));
            BufferIn = new BufferedReader(Fin);
            BufferedImage image0 = ImageIO.read(new File("fondo.jpg"));
            BufferedImage image1 = ImageIO.read(new File("Robot.jpg"));
            BufferedImage image2 = ImageIO.read(new File("Obstaculo.jpg"));
            if((this.Line = this.BufferIn.readLine()) != null){
                int aux=0;
                StringTokenizer stText= new StringTokenizer(this.Line);
                while (stText.hasMoreTokens()) {
                    String ele = stText.nextToken();
                    if(aux == 0)
                        setFil(Integer.parseInt(ele));
                    else
                        setCol(Integer.parseInt(ele));
                    aux++;
                }
        }
        while((this.Line = this.BufferIn.readLine()) != null){
            StringTokenizer stText= new StringTokenizer(this.Line);
            while (stText.hasMoreTokens()) {
                String ele = stText.nextToken();
                this.matriz[i][j].setVisitada(false); //Para poner el mapa sin estar recorrido
                    if(ele.equals(Integer.toString(Elemento.ROBOT))){
                        this.matriz[i][j].setNumImage(Elemento.ROBOT);
                        this.matriz[i][j].setImage(image1);
                        this.matriz[i][j].setEstado(false);
                        Panel.removeVectorElement("Robot");
                    }else if(ele.equals(Integer.toString(Elemento.OBSTACULO))){
                        this.matriz[i][j].setNumImage(Elemento.OBSTACULO);
                        this.matriz[i][j].setImage(image2);
                        this.matriz[i][j].setEstado(false);
                        Panel.removeVectorElement("OBSTACULO");
                    }else{
                        this.matriz[i][j].setNumImage(Elemento.FONDO);
                        this.matriz[i][j].setImage(image0);
                        this.matriz[i][j].setEstado(true);
                    }
                    j++;
            }
            i++;
            j=0;
    }
                                 
    } catch (Exception e) {
        e.printStackTrace();
        result = false;
    }
    return result;
}

//GUARDAR MATRIZ EN FICHERO
public boolean guardarMatriz(String namefile){
        boolean result = true;
        try {
	        this.Fout = new FileWriter(new File(namefile));
	        this.BufferOut = new PrintWriter(Fout);
	        this.BufferOut.println(Integer.toString(this.getFil()) + " " + Integer.toString(this.getCol()));
        for (int i = 0; i < fil; i++){
        	for (int j = 0; j < col; j++){
        		if(matriz[i][j].getNumImage() == Elemento.ROBOT)
	                 this.BufferOut.print(Integer.toString(Elemento.ROBOT));
	             else if(matriz[i][j].getNumImage() == Elemento.OBSTACULO)
                	 this.BufferOut.print(Integer.toString(Elemento.OBSTACULO));
	             else
	            	 this.BufferOut.print(Integer.toString(Elemento.FONDO));
	         if(j != col - 1)
	        	 this.BufferOut.print(" ");
        	}
        	if(i != fil - 1)
        		this.BufferOut.print("\n");
	   }
	   this.BufferOut.close();
	                
       } catch (Exception e) {
    	   e.printStackTrace();
    	   result = false;
       }
	   return result;
	}
}